/**  
* @Title: ProductController.java  
* @Description: TODO(用一句话描述该文件做什么)  
* @author 张鑫
* @phone 17561783855  
* @date 2019年3月29日  
*/
package com.fortune.rainshine.service.controller;

import com.fortune.rainshine.common.BaseController;
import com.fortune.rainshine.common.service.AliOssService;
import com.fortune.rainshine.common.utils.StringUtil;
import com.fortune.rainshine.service.service.ProductService;
import com.fortune.rainshinecommon.base.AliOssParmsModel;
import com.fortune.rainshinecommon.base.MsgModel;
import com.fortune.rainshinecommon.base.ParmsModel;
import com.fortune.rainshinecommon.enums.AliOssTypeEnum;
import com.fortune.rainshinecommon.enums.ResultCode;
import com.fortune.rainshinecommon.model.ProductModel;
import com.fortune.rainshinecommon.util.OssFileNameUtils;
import com.fortune.rainshinecommon.util.PicUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.annotation.MultipartConfig;
import java.io.IOException;
import java.util.Arrays;
import java.util.UUID;

/**
 * @ClassName: ProductController
 * @Description: TODO(产品逻辑)
 * @author 张鑫
 * @phone 17561783855
 * @date 2019年3月29日
 */
@Controller
@RequestMapping("/service/product")
@MultipartConfig
public class ProductController extends BaseController {
	private final static Logger logger = LoggerFactory.getLogger(ProductController.class);
	@Autowired
	private ProductService service;
	@Autowired
	private AliOssService aliOssService;

	@RequiresPermissions("product:view")
	@RequestMapping()
	public String product() {
		return "product/product.html";
	}

	@RequestMapping("/editForm")
	public String addUser(Model model) {
		return "product/product_form.html";
	}

	@RequiresPermissions("product:add")
	@ResponseBody
	@RequestMapping("/add")
	public MsgModel add(@RequestBody ProductModel model) {
		try {
			model.setUser(this.getLoginUser());
			return service.add(model);
		} catch (Exception e) {
			logger.error("add ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	/**
	 * @Title: list
	 * @Description: 预约列表
	 * @author zhangxin
	 * @date 2019年3月26日
	 */
	@RequiresPermissions("product:view")
	@ResponseBody
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public MsgModel list(Integer page, Integer limit, String searchKey, String searchValue) {
		try {
			if (page == null) {
				page = 0;
				limit = 0;
			}
			if (StringUtil.isBlank(searchValue)) {
				searchKey = null;
			}
			ParmsModel parm = new ParmsModel(page, limit, searchKey, searchValue);
			parm.setRoleId(this.getLoginUserRole().getRoleId());
			return service.list(parm);
		} catch (Exception e) {
			logger.error("list ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	@RequiresPermissions("product:edit")
	@ResponseBody
	@RequestMapping("/update")
	public MsgModel update(@RequestBody ProductModel model) {
		try {
			model.setUser(this.getLoginUser());
			return service.update(model);
		} catch (Exception e) {
			logger.error("ProductController update ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	/**
	 * @Title: delete
	 * @Description: 删除产品
	 * @author zhangxin
	 * @phone 17561783855
	 * @date 2019年3月28日
	 */
	@RequiresPermissions("product:delete")
	@ResponseBody
	@RequestMapping("/delete")
	public MsgModel delete(@RequestBody ProductModel model) {
		try {
			return service.delete(model);
		} catch (Exception e) {
			logger.error("delete ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}

	}

	@RequiresPermissions("product:upload")
	@ResponseBody
	@RequestMapping("/upload")
	public MsgModel upload(@RequestBody MultipartFile file) throws IOException {
		if(file == null){
			return new MsgModel("请选择文件", ResultCode.FAILURE);
		}
		String originalFileName = file.getOriginalFilename();
		if(originalFileName == null || !originalFileName.contains(".")){
			return new MsgModel("文件格式不正确", ResultCode.FAILURE);
		}
		String prefix = originalFileName.substring(originalFileName.lastIndexOf('.')).toLowerCase();
		String[] picTypes = {".jpg",".bmp",".png",".jpeg"};
		if (!Arrays.asList(picTypes).contains(prefix)) {
			return new MsgModel("请上传jpg/bmp/png/jpeg格式的图片!", ResultCode.FAILURE);
		}
//		logger.info("file size ====> {}", file.getSize());
		if(file.getSize()/1024 > (double) 300){
			return new MsgModel("上传图片不能大于300KB!", ResultCode.FAILURE);
		}
		try {
			String floder = AliOssTypeEnum.getValue(1);
			String rannum = UUID.randomUUID().toString().replaceAll("-", "");
			String fileName = OssFileNameUtils.getNewName(file, floder, rannum);
			byte[] bytes = PicUtils.quality(file.getBytes(), 0.2f);
			AliOssParmsModel model = new AliOssParmsModel(rannum, floder, fileName, bytes);
			aliOssService.uploadFile(model);
			String path = OssFileNameUtils.getFilePath(floder, fileName);
			String filepath = aliOssService.getUrl(path);
			return new MsgModel(ResultCode.SUCCESS, filepath);
		} catch (Exception e) {
			logger.error("upload ====> {}", e);
			return new MsgModel(ResultCode.FAILURE);
		}
	}

}
